package com.youlu.campus.admin.utils;


import com.youlu.campus.admin.auth.entity.SystemUser;
import com.youlu.campus.admin.auth.entity.exception.AuthErrorCode;
import com.youlu.campus.admin.auth.shiro.token.AuthPrincipal;
import com.youlu.campus.common.exception.BusinessException;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * Created by zhuhuaiqi
 */
public abstract class AuthUtils {

    protected static final Logger logger = LoggerFactory.getLogger(AuthUtils.class);

    public static SystemUser getUser() {
        AuthPrincipal authPrincipal = getAuthPrincipal();
        SystemUser authUser = new SystemUser();
        authUser.setId(authPrincipal.getUid());
        authUser.setLoginName(authPrincipal.getLoginName());
        authUser.setTenantId(authPrincipal.getComId());
        authUser.setDomain(authPrincipal.getDomain());
        //authUser.setRoles(authPrincipal.getRoles());
        return authUser;
    }

    public static String getUserId() {
        return getAuthPrincipal().getUid();
    }

    public static String getComId() {
        return "5fc5e0c6b8016b1f865aa02f";
//        return getAuthPrincipal().getComId();
    }

    public static String getLoginName() {
        return getAuthPrincipal().getLoginName();
    }

    public static String getDomain() {
        return getAuthPrincipal().getDomain();
    }

    public static Set<String> getRoles() {
        return getAuthPrincipal().getRoles();
    }

    public static AuthPrincipal getAuthPrincipal() {
        Subject subject = getSubject();
        return (AuthPrincipal) subject.getPrincipals().getPrimaryPrincipal();
        /*AuthPrincipal authPrincipal = new AuthPrincipal();
        authPrincipal.setLoginName("admin");
        authPrincipal.setUid(1008L);
        authPrincipal.setComId(1000L);
        authPrincipal.setDomain("test");
        authPrincipal.setGmtCreate(System.currentTimeMillis());
        authPrincipal.setExpiresIn(24 * 60 * 60); //
        return authPrincipal;*/
    }

    private static Subject getSubject() {
        Subject subject = SecurityUtils.getSubject();
        if (Objects.isNull(subject)) {
            logger.error(":>>> 用户没有登陆,返回登陆页");
            throw new BusinessException(AuthErrorCode.USER_NOT_LOGIN);
        }
        if (!subject.isAuthenticated()) {
            logger.warn(":>>> 用户没有授权,返回登陆页");
            throw new BusinessException(AuthErrorCode.UNAUTHORIZED);
        }
        return subject;
    }
    private String getHttpHeader(String key){
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        // get the request
        HttpServletRequest request1 = requestAttributes.getRequest();
        String sai=request1.getHeader(key);
        return sai;
    }
}
